'Copyright 2006 James Downey

Imports FetchXML
Imports FetchXMLBuilder.MetadataServiceSdk

Public Class frmFilter

    Private Filter As Filter
    Private Entity As EntityMetadata

    Public Sub New(ByVal Filter As Filter, ByVal Entity As EntityMetadata)
        InitializeComponent()
        Me.Filter = Filter
        Me.Entity = Entity
    End Sub

    Private Sub frmFilter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Filter.Type = FilterType.TypeAnd Then
            Me.radAnd.Checked = True
        Else
            Me.radOr.Checked = True
        End If
        LoadConditions()
        LoadFilters()
    End Sub

    Private Sub LoadConditions()
        Me.lstConditions.Items.Clear()
        Dim condition As Condition
        For Each condition In Me.Filter.Conditions
            Me.lstConditions.Items.Add(condition)
        Next
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        'If Me.lstConditions.Items.Count = 0 Then
        '    Windows.Forms.MessageBox.Show("The filter must contain at least one condition.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        '    Exit Sub
        'End If
        If Me.radAnd.Checked Then
            Me.Filter.Type = FilterType.TypeAnd
        Else
            Me.Filter.Type = FilterType.TypeOR
        End If
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim condition As New Condition()
        Dim frmCondition As New frmCondition(condition, Entity)
        If frmCondition.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.Filter.Conditions.Add(condition)
            LoadConditions()
        End If
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If Me.lstConditions.SelectedIndex = -1 Then
            Windows.Forms.MessageBox.Show("Select a condition.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        Dim condition As Condition = Me.lstConditions.SelectedItem
        Dim frmCondition As New frmCondition(condition, Entity)
        If frmCondition.ShowDialog() = Windows.Forms.DialogResult.OK Then
            LoadConditions()
        End If
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If Me.lstConditions.SelectedIndex = -1 Then
            Windows.Forms.MessageBox.Show("Select a condition.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        If Not Me.lstConditions.SelectedIndex = -1 Then
            If Windows.Forms.MessageBox.Show("Are you sure that you want to delete this condition?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                Me.Filter.Conditions.Remove(Me.lstConditions.SelectedItem)
                LoadConditions()
            End If
        End If
    End Sub

    Public Sub LoadFilters()
        Me.lstFilters.Items.Clear()
        Dim filter As Filter
        For Each filter In Me.Filter.Filters
            Me.lstFilters.Items.Add(filter)
        Next
    End Sub

    Private Sub btnAddFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddFilter.Click
        Dim filter As New Filter()
        Dim frmFilter As New frmFilter(filter, GetEntity(Me.Entity.Name))
        If frmFilter.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.Filter.Filters.Add(filter)
        End If
        LoadFilters()
    End Sub

    Private Sub btnEditFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditFilter.Click
        Dim filter As Filter = Me.lstFilters.SelectedItem
        Dim frmFilter As New frmFilter(filter, GetEntity(Me.Entity.Name))
        If frmFilter.ShowDialog() = Windows.Forms.DialogResult.OK Then
            LoadFilters()
        End If
    End Sub

    Private Sub btnDeleteFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteFilter.Click
        If Me.lstFilters.SelectedIndex = -1 Then
            Windows.Forms.MessageBox.Show("Select a filter.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        If Windows.Forms.MessageBox.Show("Are you sure that you want to delete this filter?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Me.Filter.Filters.Remove(Me.lstFilters.SelectedItem)
            LoadFilters()
        End If
    End Sub

End Class